#include <vector>
#include <queue>

using namespace std;

class Node
{
public:
    int val;
    vector<Node *> children;

    Node() {}

    Node(int _val)
    {
        val = _val;
    }

    Node(int _val, vector<Node *> _children)
    {
        val = _val;
        children = _children;
    }
};

class Solution
{
public:
    vector<vector<int>> levelOrder(Node *root)
    {
        queue<Node *> q;
        vector<vector<int>> ret;
        if (root == nullptr)
        {
            return ret;
        }

        int levelsize = 1;
        q.push(root);
        while (levelsize)
        {
            vector<int> tmp;
            while (levelsize--)
            {
                Node *front = q.front();
                q.pop();
                tmp.push_back(front->val);
                for (int i = 0; i < front->children.size(); ++i)
                {
                    q.push(front->children[i]);
                }
            }
            ret.push_back(tmp);
            levelsize = q.size();
        }
        return ret;
    }
};